package com.example.lyl.yuejian.view.my;

import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.Toast;

import com.example.lyl.yuejian.R;
import com.example.lyl.yuejian.adapter.MatchOrderLvAdapter;
import com.example.lyl.yuejian.bean.MatchOrderLvBean;
import com.example.lyl.yuejian.bean.ResultBean;
import com.example.lyl.yuejian.net.NetTool;
import com.example.lyl.yuejian.util.SharePreferenceUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class MyMatchOrderListActivity extends AppCompatActivity {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.orderlist_lv)
    ListView orderlistLv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_match_order_list);
        ButterKnife.bind(this);

        toolbar.setTitle("我的购票");
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }

        final MatchOrderLvAdapter adapter = new MatchOrderLvAdapter();
        orderlistLv.setAdapter(adapter);

        getData(adapter);

        adapter.setListener(new MatchOrderLvAdapter.BtnClickListener() {
            @Override
            public void btnClickLisener(int tid) {
                NetTool.getInstance().getApi()
                        .payMatchOrder(tid)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Consumer<ResultBean>() {
                            @Override
                            public void accept(ResultBean resultBean) throws Exception {
                               getData(adapter);
                            }
                        }, new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                Toast.makeText(MyMatchOrderListActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                            }
                        });
            }
        });
    }

    private void getData(final MatchOrderLvAdapter adapter) {
        NetTool.getInstance().getApi()
                .myticketsRequest(SharePreferenceUtils.getInt(MyMatchOrderListActivity.this,"uid",0))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<MatchOrderLvBean>() {
                    @Override
                    public void accept(MatchOrderLvBean matchOrderLvBean) throws Exception {
                        adapter.setMatchOrderLvBean(matchOrderLvBean);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        Toast.makeText(MyMatchOrderListActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                    }
                });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
        }
        return true;
    }
}
